\chapter{Test Evaluation}
In this chapter we will show the board game Tic-Tac-Toe and Connect 4 implemented. We will go through the different important scenarios in the two board games, to get a better understanding about how to make and structure a board game in our language. In order to show this we examine several code examples. Because TTT is a simple game, we will show the entire code. 

\section{Tic-Tac-Toe}
%We will now show some code examples from TTT.
A good place to start is the board. We made a board with 3 times 3 cells with the code in listing \ref{TTT:board:example:code}.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the board from TTT},
				label={TTT:board:example:code}]	
board MainBoard
{
	CellType = MainCell;
	MakeRect(3,3);
}
\end{lstlisting}

It is important to look at the MainCell and it's properties, because
we do not want the pieces to be stackable. In order to do so we set the property \textit{MaxPieces} to one for
the MainCell. See listing \ref{TTT:MainCell:example:code}.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the MainCell from TTT},
				label={TTT:MainCell:example:code}]	
cell MainCell
{
	MaxPieces = 1;
}
\end{lstlisting} 

The next thing we will look at are players. We can implement all the players we want, we
just need to declare a new player object for each player we want. In TTT
we need two players and their pieces. The code in listing \ref{TTT:Player:example:code} shows how to make two declarations of player objects and assigning their pieces to them.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the players from TTT},
				label={TTT:Player:example:code}]	
player XPlayer
{
	MainPiece = XPiece;
}

player OPlayer
{
	MainPiece = OPiece;
}
\end{lstlisting}
	
We assign pieces to the player by declaring piece objects and settings icons to both pieces. The code in listing \ref{TTT:Piece:example:code} show how this is done.  

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the pieces from TTT},
				label={TTT:Piece:example:code}]
piece XPiece
{
	Icon = "x.png";
}
piece OPiece
{
	Icon = "o.png";
}
\end{lstlisting}

Now we need to define the start action, the round, the turn and the action \textit{CheckWin}. The action \textit{start} uses the action \textit{UseRound} to call a round. \textit{Start} is very useful to implement some functionality before the game starts. Since there is nothing in the start conditions in
TTT, the only action in start is \textit{UseRound}, as you see in listing \ref{TTT:start:example:code}.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the start from TTT},
				label={TTT:start:example:code}]
start
{
	UseRound(MainRound);
}
\end{lstlisting}

The \textit{MainRound} is of the type round, which we need to define as: round MainRound. The MainRound in TTT is simple, since there only are two players. The \textit{MainRound} only needs to keep track of the two players' turns.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the MainRound from TTT},
				label={TTT:MainRound:example:code}]
round MainRound
{
	DoTurn(MainTurn, XPlayer);
	DoTurn(MainTurn, OPlayer);
}
\end{lstlisting}
	
The next step is to declare the \textit{MainTurn}. The \textit{MainTurn} contains any move a player can do.
The current player needs to select a cell which is not full. A cell is full if
the property MaxPieces is equal to the number of piece in the cell. If no cell can be
selected the game should end, else a piece should be placed. The last action in a turn is the action call of \textit{CheckWin}. See listing \ref{TTT:MainTurn:example:code} for the \textit{MainTurn}.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the MainTurn from TTT},
				label={TTT:MainTurn:example:code}]
turn MainTurn
{
	piece currentPiece = CurrentPlayer->MainPiece;
	cell currentCell = SelectCell("Full", false);
	if (currentCell is null)
	{
		EndGame();
	}
	PlacePiece(currentPiece, currentCell);
	CheckWin();
}	
\end{lstlisting}

The last element is the action \textit{CheckWin}. The \textit{CheckWin} action checks if a player has three pieces in a line,
if this is the case the player is set to win and the game ends.
With the code for TTT, an empty board is being displayed, this is the start condition for TTT.

\begin{figure}[h!t]
	\begin{center}
	\includegraphics[width=0.4\textwidth]{./Test_evaluation/TTTEmpty.pdf}
	\end{center}
	\caption{Shows the empty $3 \times 3$ board of Tic-Tac-Toe}
	\label{fig:TTTEmptyBoard}
\end{figure}

\begin{figure}[h!]
\centering
\subfigure[TTT game, ends in a draw]{
\includegraphics[width=0.4\textwidth]{./Test_evaluation/TTTGameEnd.pdf}
\label{fig:TTTGameEnd}
}
\subfigure[TTT game, X pieces wins]{
\includegraphics[width=0.4\textwidth]{./Test_evaluation/TTTGameEndXWin.pdf}
\label{fig:TTTGameEndXWin}
}
\caption{TTT Game Endings}
\end{figure}

The player with the ``X'' piece, player X, has the first turn. The player with the ``O'' piece, player O, has the next turn. Each player
simply uses the mouse and clicks a cell to place a piece. All cells will be occupied after only a few rounds. The game can end in three
different conditions: player X wins, player O wins or the game can end in a draw. In figures \ref{fig:TTTGameEnd} and \ref{fig:TTTGameEndXWin} are two of
the conditions. The first condition is a draw. In the second, player X won. The player with the O pieces can also win, but this is not shown in a figure.

\section{Connect 4}
Now we explain how Connect 4 would look implemented. Connect 4 starts just like TTT, but the board is larger. As you can see on figure \ref{fig:Connect4EmptyBoard}, the board starts out empty.

\begin{figure}[h!]
\centering
\subfigure[Empty $7 \times 6$ board, showing the start condition]{
\includegraphics[width=0.4\textwidth]{./Test_evaluation/Connect4EmptyBoard.pdf}
\label{fig:Connect4EmptyBoard}
}
\subfigure[Connect 4, where the game is not yet finished. Notice four of the rows are full and no pieces can be placed here]{
\includegraphics[width=0.4\textwidth]{./Test_evaluation/Connect4GameNotDone.pdf}
\label{fig:Connect4GameEndsNotDone}
}
\caption{Connect 4 states}
\end{figure}

After the game is started the two players take turns placing a piece by using the mouse to select a cell. Figure \ref{fig:Connect4GameEndsNotDone} shows how the board looks half way into the game. The gray cells indicate that the user can not place a piece
in this row, because it is full.

\begin{figure}[h!]
\centering
\subfigure[Connect4, because the end condition is met, the player with the black pieces wins]{
\includegraphics[width=0.4\textwidth]{./Test_evaluation/Connect4GamesDoneBlackWin.pdf}
\label{fig:Connect4GameEndsBlackWin}
}
\subfigure[Connect4, because no more pieces can be placed the game ends in a draw]{
\includegraphics[width=0.4\textwidth]{./Test_evaluation/Connect4GameDoneDraw.pdf}
\label{fig:Connect4GameEndsInDraw}
}
\caption{Connect 4 endings}
\end{figure}

Connect 4 can end in three different conditions, the player who controls the black pieces wins, the player with the red pieces wins or the game can end in a draw. In figure \ref{fig:Connect4GameEndsBlackWin} and in figure \ref{fig:Connect4GameEndsNotDone} are the two conditions where the player with the black pieces wins and where the game ends in a draw.
As you can see all the cells are gray, indicating that no more pieces can be placed, when the game ends.

\section{Checkers}
We will describe some implemented parts of Checkers. In Checkers a piece can
become a king piece, thus a player can have two types of pieces. We have to take into account that a piece can become a king at the end of a turn.

The first action we will look at is the \textit{MainCell}. \textit{MainCell} differs from the \textit{MainCell} action we have seen in TTT and Connect 4, since, in Checkers, it needs a custom boolean variable to check if a cell can be selected. In listing \ref{Checkers:MainCell:example:code} we see the code for modified \textit{MainCell}.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the MainCell from Checkers},
				label={Checkers:MainCell:example:code}]
cell MainCell
{
	bool canSelect = false;
	MaxPieces = 1; 
}
\end{lstlisting}

The next important part in Checkers are the players. The normal piece is set to the \textit{MainPiece} and the
King is set to the \textit{KingPiece}. Note that \textit{MainPiece} is a standard variable, but \textit{KingPiece} needs to be declared in the player definitions.
The \textit{Opponent} is a variable of the type player. It is a way to describe the opposing player. The integer \textit{MoveDirection} indicates which way the player's pieces moves. This helps solving the complexity of piece movement in the turns. The code for the two players can be seen in listing \ref{Checkers:player:example:code}.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the players from Checkers},
				label={Checkers:player:example:code}]
player Player1
{
	MainPiece = Player1Piece;
	piece KingPiece = Player1King;
	player Opponent = Player2;
	int MoveDirection = -1;
}

player Player2
{
	MainPiece = Player2Piece;
	piece KingPiece = Player2King;
	player Opponent = Player1;
	int MoveDirection = 1;
}
\end{lstlisting}

We will now take a look at the most complex part of Checkers: the turn definition. 
In the turn, the first thing to do is checking if any piece can jump over an opponent's piece. The boolean variable \textit{mustJump} is set to true if the current player has a piece which can jump. As you can see in listing ref{Checkers:MainTurn:example:code:p1} we use two while loops as for loops, by adding 1 to the counter at the end. We use the action \textit{CheckForJump} to check if a piece can jump over an opponent's piece.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the MainTurn from Checkers part 1},
				label={Checkers:MainTurn:example:code:p1}]
turn MainTurn
{
	int boardX = 0;
	int boardY = 0;
	bool mustJump = false;
	
	cell currentCell;
	
	while(not (boardX is 9))
	{
		while(not (boardY is 9))
		{			
			currentCell = GetCellAt(boardX, boardY);
			if (CheckForJump(currentCell))
			{
				currentCell->canSelect = true;
				mustJump = true;
			}
			boardY = boardY + 1;
		}
		boardX = boardX + 1;
	}
	...
\end{lstlisting}

The next part of the turn, as seen in listing \ref{Checkers:MainTurn:example:code:p2}, is executed if none of the player's pieces can jump. We need to go through all cells to find the pieces which we can move. Because king pieces can move back diagonal to the left and right,
we need to check the cell behind the current cell's cells diagonal to the left and right, if the piece we are looking . We solve
this challenge be saying $MoveDirection*(-1)$.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the MainTurn from Checkers part 2},
				label={Checkers:MainTurn:example:code:p2}]
...
boardX = 1;
boardY = 1;

if(not mustJump)
{
	while(not (boardX is 9))
	{
		while(not (boardY is 9))
		{
			if (GetFirstPiece(GetCellAt(boardX, boardY))->Owner is CurrentPlayer)
			{
				if(not GetCellAt(boardX-1, boardY + CurrentPlayer->MoveDirection)->Full)
				{
					currentCell->canSelect = true;
				}
				
				if(not GetCellAt(boardX+1, boardY + CurrentPlayer->MoveDirection)->Full)
				{
					currentCell->canSelect = true;
				}
				
				if(GetFirstPiece(currentCell) is KingPiece) //for king piece
				{
					if(not GetCellAt(boardX-1, boardY + CurrentPlayer->MoveDirection*(-1))->Full)
					{
						currentCell->canSelect = true;
					}
					if(not GetCellAt(boardX+1, boardY + CurrentPlayer->MoveDirection*(-1))->Full)
					{
						currentCell->canSelect = true;
					}
				}
				
			}
			
			boardY = boardY + 1;
		}				
		boardX = boardX + 1;
	}
}
...
\end{lstlisting}

We now know which cells can be selected. The game now waits for the user's input, as seen in listing \ref{Checkers:MainTurn:example:code:p3}. Note that the action \textit{SelectCell} returns
null if no cells can be selected. We need this to check if the player can not move. In the case where the
player can not move, the player will lose the game.

Later in the code we need to select a cell where a piece can be moved to, so a clean up of the board is needed. A
clean up in this context meaning that the property \textit{canSelect} for all cells is set to false.
At this point there can be two scenarios. The first scenario is that we do not jump. The second scenario
is that we must perform a jump. In the first scenario we check which direction the piece can move.
In the second scenario we use the action \textit{SetJumpTarget} to set the cells' property \textit{canSelect}
where the selected piece can jump to.

\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the MainTurn from Checkers part 3},
				label={Checkers:MainTurn:example:code:p3}]
...
currentCell = SelectCell("canSelect", true);

if(currentCell is null) // if a player can't move
{
	Win(CurrentPlayer->Opponent);
	EndGame();
}

CleanUpBoard();

if(not mustJump)
{
	if(not GetCellAt(currentCell->X-1, currentCell->Y +CurrentPlayer->MoveDirection)->Full)
	{
		GetCellAt(currentCell->X-1, currentCell->Y + CurrentPlayer->MoveDirection)->canSelect = true;
	}
	if(not GetCellAt(currentCell->X+1, currentCell->Y +CurrentPlayer->MoveDirection)->Full)
	{
		GetCellAt(currentCell->X+1, currentCell->Y + CurrentPlayer->MoveDirection)->canSelect = true;
	}		
	if(currentPiece is kingPiece) //for king piece
	{
		if(not GetCellAt(currentCell->X-1, currentCell->Y + CurrentPlayer->MoveDirection*(-1))->Full)
		{
			GetCellAt(currentCell->X-1, currentCell->Y + CurrentPlayer->MoveDirection*(-1))->canSelect = true;				
		}
		if(not GetCellAt(currentCell->X+1, currentCell->Y + CurrentPlayer->MoveDirection*(-1))->Full)
		{
			GetCellAt(currentCell->X+1, currentCell->Y + CurrentPlayer->MoveDirection*(-1))->canSelect = true;				
		}
	}
}

if(mustJump)
{
		SetJumpTarget(currentCell);
}
...
\end{lstlisting}

We already know which cell we are moving from, as well as the possible cells we can jump to. This means that we can now let the user select the cell which we want to move to, and then perform the action \textit{MoveAllPieces}. MoveAllPieces moves all the pieces from one cell to another. After we have moved a piece, we update the \textit{currentCell} and remove
the jumped piece from the board by using the action \textit{RemoveJumpedPiece}, if a jump was performed. \textit{RemoveJumpedPiece} removes a piece
depending on the cell the piece jumped from and on the cell the piece jumped to. We need to do a clean up
on the board since we want to make sure that a piece can jump multiple times if possible.
We use a loop to make sure the piece keeps jumping. The action \textit{CheckForJump} checks if a piece can jump,
the result is saved in \textit{mustJump}. If \textit{mustJump} is true, the action \textit{SetJumpTarget} is called. \textit{SetJumpTarget}
sets the \textit{canSelect} for all the cells the piece can jump to. Now we select a cell the piece should jump to
and perform all the necessary effects related to the jump. Now we need to check if the player can not jump after
his jump. When the piece can not jump anymore, we break out of the while loop. Then check if the piece should become
a king. 
The last thing we need to check before the turn is over is if any of the player's piece's can move.
If all his pieces is stuck, he will lose the game. 


\begin{lstlisting}[basicstyle=\small\sffamily, 
				keywords={ using, library, game, action, gives, takes, start, if, check, case, while, for, board, cell, piece, player, round, turn, int, string, true, false, bool, true, false, bool, true, false, bool},
				keywordstyle={\color{blue}},
				comment={[l]{//}}, morecomment={[s]{/*}{*/}}, commentstyle=\itshape,
				columns={[l]flexible}, numbers=left, numberstyle=\tiny,
				frameround=fftt, frame=shadowbox, captionpos=b,
				caption={Code Example of the MainTurn from Checkers part 4},
				label={Checkers:MainTurn:example:code:p4}]
	cell targetCell = SelectCell("canSelect", true);
	
	MoveAllPieces(currentCell,targetCell);
	if (mustJump)
	{
		RemoveJumpedPiece(currentCell,targetCell);
	}
	currentCell = targetCell;
	CleanUpBoard();
	
	// Loop checking for more needed jumps
	while(mustJump)
	{
		mustJump = CheckForJump(currentCell);
		
		if(mustJump)
		{
			SetJumpTarget(currentCell);
		}
		
		targetCell = SelectCell("canSelect", true);

		MoveAllPieces(currentCell,targetCell);
		RemoveJumpedPiece(currentCell,targetCell);
		currentCell = targetCell;
		CleanUpBoard();
	}
	
	CheckForKing();
\end{lstlisting}


\section{Conclusion}
The TTT and Connect4 games look a lot like each other, but they are different enough to show that a variety
board game can be written in CLUBs. Connect 4 stands out from TTT by having a win condition
that requires four of the same piece in a row. A more interesting element is that some cells can not be
placed a piece in the start of the game. This makes the game more fun to play and to program,
because the game is more dynamic than TTT. Checkers differs a lot from TTT and Connect 4 and was implemented to show that CLUBs can support a variety of board games, and also that it is possible to implement games more advanced than TTT and Connect 4.

Furthermore, we can conclude that CLUBs does not live up to its goal of making board game prototyping easy. As a board game, Checkers is considered relatively simple. The implementation of Checkers in CLUBs, however, needed quite extensive programming despite the simple nature of the game.